//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MarketModelExerciseValue.h"
using namespace Cephei::QL::Models::Marketmodels::Callability;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (boost::shared_ptr<QuantLib::MarketModelExerciseValue>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelExerciseValue = NULL;
#endif
	_ppMarketModelExerciseValue = &childNative;
    
}
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (QuantLib::MarketModelExerciseValue& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMarketModelExerciseValue = NULL;
#endif
	_ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (&childNative);
    
    _MarketModelExerciseValueOwner = owner;
    
}

Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (CMarketModelExerciseValue^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelExerciseValue = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (copy->GetShared());
        
    }
}
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelExerciseValue = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelExerciseValue::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (QuantLib::Handle<QuantLib::MarketModelExerciseValue>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelExerciseValue = &childNative;
	_ppMarketModelExerciseValue = &static_cast<boost::shared_ptr<QuantLib::MarketModelExerciseValue>>(childNative.currentLink());
    
    _MarketModelExerciseValueOwner = owner;
}
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (QuantLib::Handle<QuantLib::MarketModelExerciseValue> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMarketModelExerciseValue = &childNative;
	_ppMarketModelExerciseValue = &static_cast<boost::shared_ptr<QuantLib::MarketModelExerciseValue>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::CMarketModelExerciseValue (QuantLib::MarketModelExerciseValue childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMarketModelExerciseValue = NULL;
#endif
	_ppMarketModelExerciseValue = new boost::shared_ptr<QuantLib::MarketModelExerciseValue> (new QuantLib::MarketModelExerciseValue (childNative));
    
}
#endif

Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::~CMarketModelExerciseValue ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMarketModelExerciseValue != NULL)
    {
	    delete _ppMarketModelExerciseValue;
        _ppMarketModelExerciseValue = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::!CMarketModelExerciseValue ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMarketModelExerciseValue != NULL)
    {
	    delete _ppMarketModelExerciseValue;
    }
}
QuantLib::MarketModelExerciseValue& Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::GetReference ()
{
    if (_ppMarketModelExerciseValue == NULL) throw gcnew NativeNullException ();
	return **_ppMarketModelExerciseValue;
}
boost::shared_ptr<QuantLib::MarketModelExerciseValue>& Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::GetShared ()
{
    if (_ppMarketModelExerciseValue == NULL) throw gcnew NativeNullException ();
	return *_ppMarketModelExerciseValue;
}
QuantLib::MarketModelExerciseValue* Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::GetPointer ()
{
    if (_ppMarketModelExerciseValue == NULL) throw gcnew NativeNullException ();
	return &**_ppMarketModelExerciseValue;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelExerciseValue>& Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::GetHandle ()
{
	if (_phMarketModelExerciseValue == NULL)
	{
		_phMarketModelExerciseValue = new Handle<QuantLib::MarketModelExerciseValue> (*_ppMarketModelExerciseValue);
	}
	return *_phMarketModelExerciseValue;
}
#endif
bool Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::HasNative () 
{
	return (_ppMarketModelExerciseValue != NULL);
}

Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelExerciseValue)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::IMarketModelExerciseValue^ Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::NextStep (Cephei::QL::Models::Marketmodels::ICurveState^ prm1)
{
    CCurveState^ _Cprm1;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cprm1 = safe_cast<CCurveState^> (prm1);
        _Cprm1->Lock();
        QuantLib::CurveState& _prm1 = static_cast<QuantLib::CurveState&> (_Cprm1->GetReference ()); 
    	(*_ppMarketModelExerciseValue)->nextStep ( _prm1 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::NumberOfExercises::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelExerciseValue)->numberOfExercises ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelExerciseValue)->possibleCashFlowTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Callability::IMarketModelExerciseValue^ Cephei::QL::Models::Marketmodels::Callability::CMarketModelExerciseValue::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelExerciseValue)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

